var app = getApp();
Page({
  data: {
    commentList: [],
    loading: false,
    nodata: false,
    nomore: false,
  },
  page: 1,
  onLoad: function () {
    this.page = 1;
    this.loadComment();
  },

  onShow: function () {
    if (!app.globalData.userInfo) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }
  },
    see: function (e) {
        var see_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../see/index?see_id='+see_id
        })
    },
  onPullDownRefresh: function () {
    this.setData({ nodata: false, nomore: false });
    this.page = 1;
    this.loadComment();
  },
  onReachBottom: function () {
    var that = this;
    this.loadComment(function (data) {
      if (data.commentList.length == 0) {
        app.info("暂无更多数据");
      }
    });
  },
  loadComment: function (cb) {
    var that = this;
    if (that.data.nomore == true || that.data.loading == true) {
      return;
    }
    this.setData({ loading: true });
    app.request('/my/collection', { page: this.page }, function (data, ret) {
        var vote = wx.getStorageSync("vote") || [];
        for (var index in data.commentList) {
            if (vote.indexOf(data.commentList[index].archives.id)>-1){
                data.commentList[index].archives.is_likes = 1;
            }
        }
      that.setData({
        loading: false,
        nodata: that.page == 1 && data.commentList.length == 0 ? true : false,
        nomore: that.page > 1 && data.commentList.length == 0 ? true : false,
        commentList: that.page > 1 ? that.data.commentList.concat(data.commentList) : data.commentList,
      });
      that.page++;
      typeof cb == 'function' && cb(data);
    }, function (data, ret) {
      that.setData({
        loading: false
      });
      app.error(ret.msg);
    });
  },
    /**
     * 弹窗
     */
    showDialogBtn: function (event) {
        var aid = event.currentTarget.dataset.id;
        this.setData({
            aid: aid,
            showModal: true
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {
    },
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
        this.setData({
            showModal: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
        this.hideModal();
    },
    /**
     * 获取评论内容
     */
    getPinglun: function (e) {
        var val = e.detail.value;
        this.setData({
            pinglun: val
        });
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function (event) {
        var that = this;
        var pid = event.currentTarget.dataset.pid;
        if (!app.globalData.userInfo) {
            app.error('请登录后再评论');
            return;
        }
        if (this.data.pinglun == '') {
            app.error('内容不能为空');
            return;
        }
        app.request('/comment/post', { aid: this.data.aid, pid: 0, content: this.data.pinglun }, function (data) {
            app.success('发表成功!');
            that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
            if (that.data.commentList.length < 10) {
                that.page = 1;
            } else {
                that.data.commentList = that.data.commentList.slice(0, 10);
                that.page = 2;
            }
            that.onReachBottom();
        }, function (data, ret) {
            app.error(ret.msg);
        });
        this.hideModal();
    },
    goDetail: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../news/detail?id='+id
        })
    },
    vote: function (event) {
        var that = this;
        var archivesIndex = event.currentTarget.dataset.index;

        app.vote(event, function (data) {
            that.data.commentList[archivesIndex].archives.likes = data.likes;
            that.data.commentList[archivesIndex].archives.is_likes = 1;
            that.setData({ ['likes']:  1,'commentList':that.data.commentList});
        });
    },
    collection: function (event) {

        var that = this;
        var archivesIndex = event.currentTarget.dataset.index;

        app.collections(event, function (data) {
            that.data.commentList[archivesIndex].archives.collection = data.collection;
            that.setData({ ['collections']: 1,'commentList':that.data.commentList});
        });
    },
    qu_collection: function (event) {
        var that = this;
        var id = event.currentTarget.dataset.id;
        app.request("/archives/qu_collection", { id: id }, function (data, ret) {
            if(data.code === 1){
                app.success('取消收藏成功');
            }
            that.onLoad();
        }, function (data, ret) {
            app.error(ret.msg);
        });
    },
})